package com.zhang.modules.dict.controller;


import com.zhang.common.web.respWrapper.Res;
import com.zhang.modules.api.annotation.ApiPermission;
import com.zhang.modules.dict.entity.DictData;
import com.zhang.modules.dict.entity.DictType;
import com.zhang.modules.dict.entity.cmd.AddDataCmd;
import com.zhang.modules.dict.entity.cmd.EditDataCmd;
import com.zhang.modules.dict.entity.cmd.EditTypeCmd;
import com.zhang.modules.dict.entity.dto.DictDataDto;
import com.zhang.modules.dict.rep.DictDataRep;
import com.zhang.modules.dict.service.IDictDataService;
import com.zhang.modules.dict.service.IDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Api(tags = "字典管理")
@RequestMapping("/api/dict")
@ApiPermission("sys:dict:*")
public class DictController {

    private IDictDataService dataService;

    private IDictTypeService typeService;
    private final DictDataRep dictDataRep;

    public DictController(IDictDataService dataService, IDictTypeService typeService,
                          DictDataRep dictDataRep) {
        this.dataService = dataService;
        this.typeService = typeService;
        this.dictDataRep = dictDataRep;
    }


    @ApiOperation("字典类型详情")
    @ApiPermission("sys:dict:typeDetail")
    @GetMapping("/typeDetail")
    public Res<DictType> typeDetail(@Valid @NotNull(message = "字典类型不能为空") String dictType){
        DictType result = typeService.getByType(dictType);
        return Res.ok(result);
    }

    @ApiOperation("字典类型列表")
    @ApiPermission("sys:dict:typeList")
    @GetMapping("/typeList")
    public Res<Page<DictType>> typeList(DictType dictType, HttpServletRequest request){
        Page<DictType> result = typeService.page(dictType, request.getParameterMap());
        return Res.ok(result);
    }

    @ApiOperation("字典类型列表")
    @ApiPermission("sys:dict:AllDataByType")
    @GetMapping("/AllDataByType")
    public Res<List<DictDataDto>> AllDataByType(String dictType){
        List<DictDataDto> result = dataService.AllDataByType(dictType);
        return Res.ok(result);
    }


    @ApiOperation("字典数据列表")
    @ApiPermission("sys:dict:dataList")
    @GetMapping("/dataList")
    public Res<Page<DictDataDto>> dataList(DictData dictData, HttpServletRequest request){
        Page<DictDataDto> result = dataService.page(dictData, request.getParameterMap());
        return Res.ok(result);
    }


    @ApiOperation("添加字典类型")
    @ApiPermission("sys:dict:addType")
    @PostMapping("/addType")
    public Res addType(@RequestBody DictType type){
        typeService.save(type);
        return Res.ok("添加成功");
    }

    @ApiOperation("添加字典数据")
    @ApiPermission("sys:dict:addData")
    @PostMapping("/addData")
    public Res addType(@RequestBody AddDataCmd data){
        dataService.save(data,DictData::new);
        return Res.ok("添加成功");
    }

    @ApiOperation("修改字典类型")
    @ApiPermission("sys:dict:editType")
    @PostMapping("/editType")
    public Res editType(@RequestBody EditTypeCmd editTypeCmd){
        typeService.update(editTypeCmd);
        return Res.ok("更新成功");
    }

    @ApiOperation("修改字典数据")
    @ApiPermission("sys:dict:editData")
    @PostMapping("/editData")
    public Res editData(@RequestBody EditDataCmd editDataCmd){
        dataService.update(editDataCmd);
        return Res.ok("更新成功");
    }

    @ApiOperation("删除字典类型")
    @ApiPermission("sys:dict:deleteType")
    @GetMapping("/deleteType")
    public Res deleteType(String id){
        typeService.delete(id);
        return Res.ok("删除成功");
    }

    @ApiOperation("删除字典数据")
    @ApiPermission("sys:dict:deleteData")
    @GetMapping("/deleteData")
    public Res deleteData(String id){
        dataService.delete(id);
        return Res.ok("删除成功");
    }
}
